package com.atues.backtrack.letterCombinations;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class Main {



    List<String> res = new ArrayList<>();
    StringBuilder path = new StringBuilder();

    String[] numberChar = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

    public List<String> letterCombinations(String digits) {
        char[] chars = digits.toCharArray();
        backtracking(digits.length(), chars,  0);
        return res;

    }

    public void backtracking(int k, char[] nums, int numIndex) {
        if (path.length() == k && path.length() > 0) {
            res.add(new String(path));
            return;
        }
        if(numIndex >= nums.length)return;


        int num = nums[numIndex] - '0';
        for (int j = 0; j < numberChar[num].length(); j++) {
            path.append(numberChar[num].charAt(j));
            backtracking(k,nums,numIndex + 1);
            path.delete(path.length() - 1, path.length());
        }


    }

    public static void main(String[] args) {
        Main main = new Main();
        List<String> strings = main.letterCombinations("");
        System.out.println(JSON.toJSONString(strings));
    }
}
